﻿

using System;
using System.Collections.Generic;
using System.Data;
using OestsCommon.Model;

namespace OestsDataBase
{
    public static class PostTaskDAL
    {
        /// <summary>
        /// 任务岗位关系表 主键包含 primary key(task_id,post_id)。id 为具体的任务编号
        /// 功能：1、存储大任务（由每个岗位的任务组成）所需要的岗位；
        ///       2、存储大任务下的每个岗位的具体任务编号id
        /// </summary>
         public static void CreateTable()
        {
            string sqlCheck = "if object_id( 'T_PostTask') is not null select 1 else select 0";
            if ((int)SqlHelper.ExecuteScalar(sqlCheck) == 0)
            {
                string sqlCreacte = @"CREATE TABLE T_PostTask (id bigint not null,
                                task_code bigint not null , 
                                post_id bigint not null,
                                scene NVARCHAR(MAX),primary key(task_code,post_id))";

                SqlHelper.ExecuteNonQuery(sqlCreacte);
            }
        }    


        public static List<PostTaskModel> GetData()
         {
             List<PostTaskModel> list = new List<PostTaskModel>();
             string strSql = "select * from T_PostTask";
             DataTable dt = SqlHelper.ExecuteDataTable(strSql);
             if (dt == null) { return null; }
             foreach (DataRow dr in dt.Rows)
             {
                 PostTaskModel model = new PostTaskModel();
                 model.PostId = Int32.Parse(dr["post_id"].ToString());
                 model.TaskId = Int32.Parse(dr["task_id"].ToString());
                 model.SceneName = dr["scene"].ToString();
                 list.Add(model);
             }
             return list;
         }
        public static List<int> GetPostIDsByTaskId(int taskId)
        {
            List<int> list = new List<int>();
            string strSql = "select post_id from T_PostTask where task_id = "+ taskId;
            DataTable dt = SqlHelper.ExecuteDataTable(strSql);
            if (dt == null) { return null; }
            foreach (DataRow dr in dt.Rows)
            {
                int postId = Int32.Parse(dr["post_id"].ToString());
                list.Add(postId);
            }
            return list;
        }
    }
}
